/*
 * @Author: szx
 * @Date: 2022-06-21 14:32:18
 * @LastEditTime: 2022-06-21 14:41:11
 * @Description:
 * @FilePath: \leetcode\500-599\508\508.js
 */
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
 var findFrequentTreeSum = function (root) {
    const map = new Map();
    const dfs = (root) => {
        if (!root) return 0;
        const left = dfs(root.left);
        const right = dfs(root.right);
        const sum = left + right + root.val;
        map.set(sum, (map.get(sum) || 0) + 1);
        return sum;
    };
    dfs(root)
    let max = 0;
    for (let m of map.values()) max = Math.max(m, max);
    const res = [];
    for (let m of map) if (m[1] === max) res.push(m[0]);
    return res;
};
